import { createRouter, createWebHistory } from 'vue-router'

// 登录页面
import Login from './views/Login.vue'
// 用户仪表盘页面
import UserDashboard from './views/UserDashboard.vue'

// 组织架构管理相关路由
import DepartmentManagement from './views/DepartmentManagement.vue'
import ApplicationManagement from './views/ApplicationManagement.vue'
import UserManagement from './views/UserManagement.vue'

// 主页
import Home from './views/Home.vue'

// 任务角色管理相关路由
import RoleDefinition from './views/RoleDefinition.vue'
import RouteFieldConfiguration from './views/RouteFieldConfiguration.vue'
import RoleRouteManagement from './views/RoleRouteManagement.vue'
import RoutingRules from './views/RoutingRules.vue'

// 数据源配置相关路由
import DataSourceConfiguration from './views/DataSourceConfiguration.vue'
// 表单设计器
import FormDesigner from './views/FormDesigner.vue'
// 任务流程设计器
import TaskProcessDesigner from './views/TaskProcessDesigner.vue'
// 任务管理
import TaskManagement from './views/TaskManagement.vue'
// 任务详情
import TaskDetailView from './views/TaskDetailView.vue'
// 全流程投演
import RouteSimulation from './views/RouteSimulation.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: { requiresAuth: true }
    },
    // 登录页面
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { requiresNoAuth: true }
    },
    // 组织架构管理
    {
      path: '/department-management',
      name: 'DepartmentManagement',
      component: DepartmentManagement,
      meta: { requiresAuth: true }
    },
    {
      path: '/application-management',
      name: 'ApplicationManagement',
      component: ApplicationManagement,
      meta: { requiresAuth: true }
    },
    {
      path: '/user-management',
      name: 'UserManagement',
      component: UserManagement,
      meta: { requiresAuth: true }
    },
    // 任务角色管理
    {
      path: '/role-definition',
      name: 'RoleDefinition',
      component: RoleDefinition,
      meta: { requiresAuth: true }
    },
    {
      path: '/route-field-configuration',
      name: 'RouteFieldConfiguration',
      component: RouteFieldConfiguration,
      meta: { requiresAuth: true }
    },
    {
      path: '/role-route-management',
      name: 'RoleRouteManagement',
      component: RoleRouteManagement,
      meta: { requiresAuth: true }
    },
    {
      path: '/routing-rules',
      name: 'RoutingRules',
      component: RoutingRules,
      meta: { requiresAuth: true }
    },
    // 数据源配置
    {
      path: '/data-source-configuration',
      name: 'DataSourceConfiguration',
      component: DataSourceConfiguration,
      meta: { requiresAuth: true }
    },
    // 用户仪表盘页面
    {
      path: '/user-dashboard',
      name: 'UserDashboard',
      component: UserDashboard,
      meta: { requiresAuth: true }
    },
    // 表单设计器
    {
      path: '/form-designer/:taskId?',
      name: 'FormDesigner',
      component: FormDesigner,
      meta: { requiresAuth: true }
    },
    // 任务流程设计器
    {
      path: '/task-process-designer',
      name: 'TaskProcessDesigner',
      component: TaskProcessDesigner,
      meta: { requiresAuth: true }
    },
    // 任务管理
    {
      path: '/task-management',
      name: 'TaskManagement',
      component: TaskManagement,
      meta: { requiresAuth: true }
    },
    // 任务详情
    {
      path: '/task-detail/:id',
      name: 'TaskDetailView',
      component: TaskDetailView,
      meta: { requiresAuth: true }
    },
    // 全流程投演
    {
      path: '/route-simulation',
      name: 'RouteSimulation',
      component: RouteSimulation,
      meta: { requiresAuth: true }
    }
  ]
})

// 路由守卫 - 检查用户是否已登录
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem('user');
  const isAuthenticated = !!user;
  
  // 如果路由需要认证但用户未登录，则重定向到登录页面
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login');
  }
  // 如果路由需要未认证（如登录页面）但用户已登录，则根据用户类型重定向
  else if (to.meta.requiresNoAuth && isAuthenticated) {
    try {
      const storedData = JSON.parse(user);
      const userInfo = storedData.userInfo || storedData;
      
      // 根据用户类型跳转到相应页面
      if (userInfo.userType === '管理员') {
        next('/home');
      } else {
        next('/user-dashboard');
      }
    } catch (error) {
      console.error('解析用户信息失败:', error);
      // 如果解析失败，默认跳转到登录页重新登录
      next('/login');
    }
  }
  else {
    next();
  }
});

export default router